// 处理时间
const formatTime = (date) => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return {
        y: year,
        m: formatNumber(month),
        ym: [year, month].map(formatNumber).join('-'),
        ymd: [year, month, day].map(formatNumber).join('-'),
        time: [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
    }
}
const formatNumber = (n) => {
    n = n.toString()
    return n[1] ? n : '0' + n
}


// 获取设备信息
const systemInfo = () => {
    const systemInfo = uni.getSystemInfoSync()
    // #ifdef MP 
    let capsuleInfo = uni.getMenuButtonBoundingClientRect()
    console.log('胶囊信息', capsuleInfo)
    const navbarH = capsuleInfo.height + (capsuleInfo.top - systemInfo.statusBarHeight) * 2
    console.log('微信原生计算出的导航栏高度',navbarH)
    systemInfo.navbarH = navbarH
    // #endif

    // #ifdef H5 || APP-PLUS || APP-NVUE
    systemInfo.normalContentH = systemInfo.windowHeight - systemInfo.statusBarHeight - 44
    // #endif

    console.log('设备信息',systemInfo)
    return systemInfo
}


// uni提示
const showToast = (title,icon,image,mask,duration,position) => {
    return uni.showToast({
        title: title,
        icon: icon, // none,loading,success
        image: image,
        mask: mask ,
        duration: duration,
        position: position, // top center bottom
    });
}
// uview2.x提示
const uToast = (that, type, icon, message, duration, params, cb) => {
    return that.$refs.uToast.show({
        type: type || 'primary ', // default默认、error红色、success绿色、warning黄色、info灰色、primary蓝色
        icon: icon || '',
        message: message || '安薪乐人社端',
        duration: duration || 1500,
        params: params || {},
        complete(){
            cb && cb()
        }
    })
}

// uni-app询问框
const showModal = (title, content, confirms, cancel) => {
    return uni.showModal({
        title: title,
        content: content,
        mask: true,
        cancelText: "取消", 
        confirmText: "确定",
        showCancel: true, // 是否显示取消按钮
        confirmColor: '#f55850',
        cancelColor: '#39B54A',
        success: (res) => {
            if (res.confirm) {
               confirms && confirms()
            } else {
                cancel && cancel()
            }
        }
    })
}

// 身份证脱敏
const formatIdentity = (str) => {
    return str.substr(0, 10) + "******" + str.substr(16);
}

// 将一位数组或者字符串或者对象分割成每n个一组
const transTwoArry = (str, num) => {
    var result = []
    for (var i = 0; i < str.length; i += num) {
        result.push(str.slice(i, i + num) + '\n')
    }
    return result
}



// 属地
const getNationFn = (that,cb) => {
    that.$request(that, that.$apis.CommonNation, {}).then(res => {
        res.unshift({ districtName: '全部属地', districtId: 660000  })
        const obj = { 
            label: res[0].districtName, value: res[0].districtId, show: false, type: 'districtId',
            list:[]
        }
        res.map((v, i) => {
            obj.list.push({
                label: v.districtName,
                value: v.districtId,
            })
        })
        cb && cb(obj)
    }).then()
}
// 企业类型
const getEntTypeFn = (that, cb) => {
    that.$request(that, that.$apis.entType,{}).then(res => {
        console.log('企业类型', res)
        res.unshift({
            name: '全部企业',
            value: ''
        })
        const obj = {
            label: res[0].name,
            value: res[0].value,
            show: false,
            type: 'dwlx',
            list: []
        }
        res.map((v, i) => {
            obj.list.push({
                label: v.name,
                value: v.value,
            })
        })
        cb && cb(obj)
    }).then()
}
// 项目类型
const getProTypeFn = (that,cb) => {
    that.$request(that, that.$apis.DictionariesXmtype, {}).then(res => {
        // console.log('项目类型',res)
        res.unshift({ dictLabel: '项目类型', dictValue: '' })
        const obj = {
            label: res[0].dictLabel, value: res[0].dictValue, show: false, type: 'xmtype', 
            list: []
        }
        res.map((v, i) => {
            obj.list.push({
                label: v.dictLabel,
                value: v.dictValue,
            })
        })
        cb && cb(obj)
    }).then()
}
// 项目状态
const getProStatusFn = (that,cb) => {
    that.$request(that,that.$apis.dictionariesJsjd, {}).then(res=>{
        // console.log('项目状态',res)
        res.unshift({
            dictLabel: '项目状态',
            dictValue: ''
        })
        const obj = {
            label: res[0].dictLabel,
            value: res[0].dictValue,
            show: false,
            type: 'jsjd',
            list: []
        }
        res.map((v, i) => {
            obj.list.push({
                label: v.dictLabel,
                value: v.dictValue,
            })
        })
        cb && cb(obj)
    }).then()
}
// 人员是否认证
const staffRenzhengFn = (that,cb) =>{
    that.$request(that, that.$apis.DictionariesSmrz, {}).then(res => {
        // console.log('人员是否认证',res)
        res.unshift({
            dictLabel: '全部认证状态',
            dictValue: ''
        })
        const obj = {
            label: res[0].dictLabel,
            value: res[0].dictValue,
            show: false,
            type: 'smrz',
            list: []
        }
        res.map((v, i) => {
            obj.list.push({
                label: v.dictLabel,
                value: v.dictValue,
            })
        })
        cb && cb(obj)
    }).then()
}
// 人员状态
const staffStatusFn = (that, cb) => {
    that.$request(that, that.$apis.DictionariesRyzt, {}).then(res => {
        // console.log('人员状态',res)
        res.unshift({
            dictLabel: '全部进出场状态',
            dictValue: ''
        })
        const obj = {
            label: res[0].dictLabel,
            value: res[0].dictValue,
            show: false,
            type: 'ryzt',
            list: []
        }
        res.map((v, i) => {
            obj.list.push({
                label: v.dictLabel,
                value: v.dictValue,
            })
        })
        cb && cb(obj)
    }).then()
}
// 开户银行名称
const fohfBankFn = (that, cb) => {
    that.$request(that, that.$apis.DictionariesYhdm, {}).then(res => {
        // console.log('开户银行',res)
        res.unshift({
            dictLabel: '全部开户银行',
            dictValue: ''
        })
        const obj = {
            label: res[0].dictLabel,
            value: res[0].dictValue,
            show: false,
            type: 'bankType', // yhdm
            list: []
        }
        res.map((v, i) => {
            obj.list.push({
                label: v.dictLabel,
                value: v.dictValue,
            })
        })
        cb && cb(obj)
    }).then()
}
// 业务状态
const ywztFn = (that,cb) => {
    that.$request(that, that.$apis.DictionariesYwzt, {}).then(res => {
        res.unshift({ dictLabel: '全部业务状态', dictValue: '' })
        const obj = {  
            label: res[0].dictLabel, value: res[0].dictValue, show: false, type: 'ywzt', list: []
        }
        res.map((v, i) => {
            obj.list.push({
                label: v.dictLabel, value: v.dictValue,
            })
        })
        cb && cb(obj)
    }).then()
}



module.exports = {
    formatTime, // 时间处理
    systemInfo, // 设备信息
    showToast, // uni-app提示
    uToast, // uview提示
    showModal, // uni-app询问框

    formatIdentity, // 身份证脱敏

    transTwoArry, // 将一位数组或者字符串或者对象分割成每n个一组

    getNationFn,
    getEntTypeFn,
    getProTypeFn,
    getProStatusFn,
    staffRenzhengFn,
    staffStatusFn,
    fohfBankFn,
    ywztFn

}
